﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_Gobal_MainPageMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridViewBind();

        }
    }
    private void GridViewBind()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath(@"~/XML/MenuPhotos.xml"));
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridViewBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath(@"~/XML/MenuPhotos.xml"));
        string id = ((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text;
        string url = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("url")).Text;
        string pname = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("pname")).Text;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i]["id"].ToString().Trim() == id.Trim())
            {
                ds.Tables[0].Rows[i]["url"] = url;
                ds.Tables[0].Rows[i]["pname"] = pname;
                break;
            }
        }
        try
        {
           
            ds.WriteXml(Server.MapPath(@"~/XML/MenuPhotos.xml"));
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新成功');</script>");
            GridView1.EditIndex = -1;
            GridViewBind();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新失败');</script>");
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = ((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text;
        if (Convert.ToInt32(id) == 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('此记录被保护,不能删除');</script>");
            return;
        }
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath(@"~/XML/MenuPhotos.xml"));

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            if (dr["id"].ToString().Trim() == id.Trim())
            {
                ds.Tables[0].Rows.Remove(dr);
                break;
            }
        }
        ds.WriteXml(Server.MapPath(@"~/XML/MenuPhotos.xml"));
        ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除成功');</script>");
        GridViewBind();
    }
    protected void add_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.new_pname.Text.Trim()) || string.IsNullOrEmpty(this.new_url.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请输入相册名称和链接地址');</script>");
            GridViewBind();
            return;
        }
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath(@"~/XML/MenuPhotos.xml"));
        DataRow dr = ds.Tables[0].NewRow();
        dr["pname"] = this.new_pname.Text;
        dr["url"] = this.new_url.Text;
        dr["id"] = (Convert.ToInt32(ds.Tables[0].Rows[0]["id"]) + 1).ToString();
        ds.Tables[0].Rows.InsertAt(dr, 0);
        try
        {
            ds.WriteXml(Server.MapPath(@"~/XML/MenuPhotos.xml"));
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加成功');</script>");
            this.new_pname.Text = string.Empty;
            this.new_url.Text = string.Empty;
            GridViewBind();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加失败');</script>");
        }
    }
}